from PyQt5.QtWidgets import QMessageBox
from PyQt5.uic import loadUi
from src.utils.ui_utils import center_window

class LoginView:
    """登录界面视图类"""
    
    def __init__(self, controller):
        # 保存控制器引用
        self.controller = controller
        
        # 加载UI文件
        self.ui = loadUi('src/ui/login.ui')
          # 连接信号和槽
        self.ui.loginButton.clicked.connect(self.on_login_clicked)
        self.ui.forgotPasswordButton.clicked.connect(self.on_forgot_password_clicked)
        self.ui.registerButton.clicked.connect(self.on_register_clicked)
        
        # 设置窗口居中显示
        center_window(self.ui)
        
    def show(self):
        """显示登录界面"""
        self.ui.show()
        
    def hide(self):
        """隐藏登录界面"""
        self.ui.hide()
        
    def on_login_clicked(self):
        """登录按钮点击事件"""
        phone = self.ui.phoneInput.text().strip()
        password = self.ui.passwordInput.text().strip()
        
        # 验证输入
        if not phone:
            QMessageBox.warning(self.ui, '警告', '请输入用户名！')
            self.ui.phoneInput.setFocus()
            return
        
        if not password:
            QMessageBox.warning(self.ui, '警告', '请输入密码！')
            self.ui.passwordInput.setFocus()
            return
            
        # 调用控制器处理登录逻辑
        self.controller.do_login(phone, password)
        
    def on_forgot_password_clicked(self):
        """忘记密码点击事件"""
        self.controller.show_reset_password()
        
    def on_register_clicked(self):
        """注册按钮点击事件"""
        self.controller.show_register()
        
    def show_login_success(self, message):
        """显示登录成功消息"""
        QMessageBox.information(self.ui, '登录成功', message)
        
    def show_login_error(self, message):
        """显示登录失败消息"""
        QMessageBox.critical(self.ui, '登录失败', message)